<!--
PAGE DOCUMENTATION:
==================
This page is were the recorded and pre-recorded messages are viewed.
User can handle those messages from here.
User can choose to record a new message from this page as well.

Parameters that can be sent to this page:
----------------------------------------
timeZoneSelect - if the user updated his/her time settings. 
				Value: the offset from GMT chosen by the user.
recipient - if the user got to this page after asking to respond a received message. 
			Value: friend's facebook user ID.
delete - if the user selected a message on this page and chose to delete it. 
			Value: "delete".
			Note: must come with an index parameter as well.
play - if the user selected a message on this page and chose to play it. 
			Value: "play".
			Note: must come with an index parameter as well.
index - Value: the DB index of the message the user performd an action on.
success - if the user just performed a sending operation.
			Values: "all" - when sending was succesfull. Or, the sending failed, 
			a list of the facebook user IDs of the ones the message was not sent to.
-->


<fb:dashboard>
<fb:action href="invite.php">
Invite some friends over!
</fb:action>
<fb:action href="getMessages.php">
Hear a voice message from your friends!
</fb:action>
<fb:action href="index.php">
VoiceMessanger main page!
</fb:action>
<fb:help href="help.php">
Help Me!
</fb:help>
</fb:dashboard>

<!-- creating a pre-embeded FBML block to be used while Ajax dialogs are loading-->
 <fb:js-string var="dialog_fbml">  
    <div id="dialog_content">  
        <div class="dialog_loading">Loading...</div>  
    </div>  
</fb:js-string>

<?php
	/* definition of the content of blank text field (for recording)*/
	define('BLANK_INPUT', 'Click to type'); 

	require_once 'appinclude.php';
	/* include/cache the css file */
	echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href='$server/design.css?v1.3'/>";
?>

<!-- make the BLANK_INPUT and SERVER accessible to the javascript-->
<script>
var BLANK_INPUT = '<?php echo addslashes(BLANK_INPUT); ?>'; 
var server = '<?php echo SERVER; ?>';
</script>

<?php
	/* include/cache the js file */
	echo "<script src='$server/js_features.js?v3.351'></script>";
	require_once 'sqlLib.php';
	require_once 'logsLib.php';
	require_once 'generalLib.php';
	
	$currentPage = 'sendMessage';
	$user_id = $facebook->user;
	$sqlDb = new mysqli("localhost", "root", "", "messages");

	/* check connection */
	if (mysqli_connect_errno()) {
    	documentError($user_id,$currentPage,"Connect to MYSQL failed: ".mysqli_connect_error());
    	printErrorMsg("connecting to the database");
	} else {
		$sqlDb->query('SET NAMES utf8');
		logMysqliConnectionOpened($user_id,$currentPage);
		
		/* if user changed his time settings - update the DB: */
		if (isset($_REQUEST['timeZoneSelect'])) {
			$newTimeOffset = $_REQUEST['timeZoneSelect'];
			$updateRes = updateDBcolumn($sqlDb,'sessions',$user_id,'time_offset',$newTimeOffset);
			if ($updateRes) {
				logFunctionCall($user_id,'updateDBcolumn','sqlDb,sessions,'.$user_id.',time_offset,'.$newTimeOffset,$updateRes,$currentPage);
			} else {
				documentError($user_id,$currentPage,"updateDBcolumn failed");
				printErrorMsg("updating your time settings");
			}
		}
		
		/* if the user got here via respond command */
		if (isset($_REQUEST['recipient'])) {
			$recipient = $_REQUEST['recipient'];
		} else {
			$recipient = 0;
		}
		
		/* if the user wants to delete a message or play one */
		if ((isset($_REQUEST['delete'])) || (isset($_REQUEST['play']))) {
			$index = $_REQUEST['index'];
			$filename = getColumnData($sqlDb,"messages_data",$index,'filename');
			if ($filename==-13) {
				documentError($user_id,$currentPage,"getmessageColumnData failed: index: ".$index.", column: filename");
				//$sqlDb->close();
				//logMysqliConnectionClosed($user_id,$currentPage);
				//logRedirection($user_id,"error.php?action=delete",$currentPage);
				//echo "<fb:redirect url=\"error.php?action=delete\"/>";
			} else {
				logFunctionCall($user_id,'getColumnData',"sqlDb,messages_data,".$index.",filename", $filename,$currentPage);
			}
			if (isset($_REQUEST['delete'])) {
				/* delete message */
				if ($filename!=-13) {
					if (!($deleteMsgRes=deleteMessageData($sqlDb,$index))) {
						documentError($user_id,$currentPage,"deleteMessageData failed: index: ".$index);
						printErrorMsg("deleting the message (it may not have been deleted)");
						//$sqlDb->close();
						//logMysqliConnectionClosed($user_id,$currentPage);
						//logRedirection($user_id,"error.php?action=delete",$currentPage);
						//echo "<fb:redirect url=\"error.php?action=delete\" />";
					} else {
						logFunctionCall($user_id,"deleteMessageData","sqlDb,".$index,$deleteMsgRes,$currentPage);
					}
					$deleteRes = checkFileDeleteNeeded($sqlDb,$filename);
					if ($deleteRes==1 || $deleteRes==0){
						if ($deleteRes==1) {
							$fullFileName = $filesLocation .$filename;
							$unlinkRes = unlink($fullFileName);
							logFunctionCall($user_id,'unlink',$fullFileName,$unlinkRes,$currentPage);
						}
						logFunctionCall($user_id, 'checkFileDeleteNeeded', "sqlDb,".$filename,$deleteRes,$currentPage);
					} else {
						documentError($user_id,$currentPage,"checkFileDeleteNeeded failed - filename: $filename");
						printErrorMsg("deleting the message (it may not have been deleted)");
					}
				} else {
					printErrorMsg("deleting the message (it may not have been deleted)");
				}
			} else {
				/* play a message */
				if ($filename!=-13) {
					echo "<table cellpadding=\"10\" align=\"center\"><tr><td>";
					echo "Loading file. This might take a few seconds or more (Depending on message length).<tr> ";
					activatePlayApplet($server,$filename);
					logFunctionCall($user_id, "activatePlayApplet",$server.','.$filename,NULL,$currentPage);
					echo "</td></tr></table>";
				} else {
					printErrorMsg("playing the message");
				}
			}
		}
		
		/* user got here after canceling a recording (can't tell if recording is finished)*/
		/*
		if (isset($_REQUEST['cancel'])) {
			$filename = $_REQUEST['newFileName'];
			unlink($filename);
			logFunctionCall($user_id, 'unlink', $filename, NULL,$currentPage);
		} else
		*/
		
		/* if user got here after sending a message */
		if (isset($_REQUEST['success'])) {
			$success = $_REQUEST['success'];
			if (!strcmp($success,"all")) {
				echo "<fb:success>
					<fb:message>Message Sent</fb:message>
					Your message was successfuly sent!</br>
					If you sent this message to friends that didn't add VoiceMessanger yet, it will wait for them.
					</fb:success>";
			} else {
				echo "<fb:error>
		      		<fb:message>Pay Attention!</fb:message>
		      		Your message was NOT sent to the following friends:</br>".
					getNamesList($success)."</fb:error>";
					logFunctionCall($user_id,'getNamesList',$success,NULL,$currentPage);
			}
		}
		
		/* show page content */
		echo "<table border=\"0\" align=\"center\" cellpadding=\"10\">

				<tr>
					<td colspan=\"2\" style=\"color:#3b5998;\">";
		/* get amount of messages user recorded (and are saved) */
		if (($recordedAmount = countMessages($sqlDb,$user_id,"recorded"))==-1) {
			documentError($user_id,$currentPage,"countMessages failed: user:$user_id, recorded");
			printErrorMsg("doing something. You cannot record at the moment");
		} else {
			logFunctionCall($user_id, countMessages, "sqlDb,".$user_id.",recorded", $recordedAmount,$currentPage);
			if ($recordedAmount>=10) {
				echo "<h1 class=\"tableheader\">You cannot record new messages.</h1>
						</br>
						<font size=\"3\">you can keep up to 10 recorded messages at all times.</font>
						</br>
						<font size=\"3\">If you wish to record you must first delete one of your old recordings.</font>";
				} else {
				echo "
							<div style=\"background:#c3cddf ;text-align:center;\">
							<a onmousedown=\"showDialog('$server/recordDialog.php?recipient=$recipient&user_id=$user_id','Name your message', 'ok', 'cancel');\" style=\"cursor: pointer;\"><h1 class=\"tableheader\">Click here to record a new message!</h1></a>
							</div>
							</br>
							";
				}
		}
		echo "
					</td>
				</tr>
				<tr>
					<td colspan=\"2\">
						<h1 class=\"tableheader\">Messages you recorded:</h1>
					</td>
				</tr>
				<tr>
					<td colspan=\"2\" valign=\"top\">";
		/* recorded messages table */
		if (($offsetHours = getColumnData($sqlDb,"sessions",$user_id,"time_offset"))==-13) {
			documentError($user_id,$currentPage,"getColumnData failed");
			printErrorMsg("presenting the recording time for each message (the time will be GMT)");
			$offsetHours = 0;
		} else {
			logFunctionCall($user_id,'getColumnData',"sqlDb,sessions".$user_id.",time_offset",$offsetHours,$currentPage);
		}
		
		if (!($tableRes1=presentRecordedMessages($sqlDb,$user_id,$offsetHours,$recipient))) {
			documentError($user_id,$currentPage,"presentRecordedMessages failed - user_id: ".$user_id);
			printErrorMsg("searchig the data base for your recorded messages");
		} else {
			logFunctionCall($user_id, 'presentRecordedMessages',"sqlDb,".$user_id.','.$offsetHours.','.$recipient,$tableRes1,$currentPage);
		}
		echo " 		</td>
				</tr>
				<tr>
					<td colspan=\"2\">";
		/* present table of pre-recorded messages */
		echo "			<h1 class=\"tableheader\">Other messages, for your convenience:</h1>
					</td>
				</tr>
				<tr>
					<td valign=\"top\">";
		if (!($tableRes2=presentPreRecordedMessages($sqlDb,$recipient))) {
			documentError($user_id,$currentPage,"presentPreRecordedMessages failed - recipient: ".$recipient);
			printErrorMsg("searchig the data base for the pre-recorded messages");
		} else {
			logFunctionCall($user_id,'presentPreRecordedMessages',"sqlDb,".$recipient,$tableRes2,$currentPage);
		}
		echo "		</td>
					<td>
						<img src=\"$server/tin_can_phone.jpg\" align=\"right\">
					</td>
				</tr>
							<tr>
					<td colspan=\"2\">";
		/* time settings */
		if (!($timeSettingRes = showTimeSettings($sqlDb,$user_id,$currentPage))) {
			documentError($user_id,$currentPage,"showTimeSettings failed");
		} else {
			logFunctionCall($user_id, 'showTimeSettings', "sqlDb,".$user_id.",".$currentPage,$timeSettingRes,$currentPage);
		}
		/* present table of messages recorded by user */
		echo "		</td>
				</tr>
			</table>";
    	
		/* close connection */
		$sqlDb->close();
		logMysqliConnectionClosed($user_id,$currentPage);
	}
	

?>
</br>
<font size="3">
This application participates in Unicell's contest. <a href="http://www.liveface.co.il/%D7%AA%D7%97%D7%A8%D7%95%D7%AA-%D7%90%D7%A4%D7%9C%D7%99%D7%A7%D7%A6%D7%99%D7%95%D7%AA-%D7%A4%D7%99%D7%99%D7%A1%D7%91%D7%95%D7%A7/apps/" > For rating and other applications. </a>
</font>
